<?php

namespace app\command;

use app\admin\model\TrainChild;
use FFMpeg\FFMpeg;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class SyntheticVideo extends command
{
    //设置php运行脚本名称
    protected function configure()
    {
        $this->setName('SyntheticVideo')->setDescription('合成视频');
    }

    /**
     *
     * php think SyntheticVideo
     * @param Input $input
     * @param Output $output
     */
    protected function execute(Input $input, Output $output)
    {
        header("Content-type:text/html;charset=utf-8");
        date_default_timezone_set('PRC');
        ini_set('memory_limit', '1000M');
        set_time_limit(0);
        try {
            $childInfo = TrainChild::where('train_status', 1)->find();
            if (!$childInfo) exit('无训练完成内容');
            //分割视频成图片
//            $command = "ffmpeg -i D:\programFiles\project\giteeProject/training_admin\public\upload/test3.mp4 D:\programFiles\project\giteeProject/training_admin\public\upload/train/" . $childInfo['child_id'] . "/%06d.png";
//            shell_exec($command);
//            echo "视频分割成功！";die();

            //合成图片为视频
            $imagePath = "/www/wwwroot/training_admin/public/upload/train/" . $childInfo['child_id'];
            $imageName = $imagePath . "/%06d.jpg";
            $fileName = "/www/wwwroot/training_admin/public/upload/video/train/" . md5($childInfo['child_id']) . ".mp4";
            //-vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2"
            $command = 'ffmpeg -fflags +genpts -framerate 30 -i ' . $imageName . ' -c:v libx264 -pix_fmt yuv420p ' . $fileName;
            shell_exec($command);
            if (file_exists($fileName)) {
                $childInfo->save(['train_status' => 2, 'train_video' => "/upload/video/train/" . md5($childInfo['child_id']) . ".mp4"]);
                $files = glob($imagePath . '/*');
                foreach ($files as $file) {
                    unlink($file);
                }
                rmdir($imagePath);
            }
            echo "视频合成成功！";
        } catch (\Exception $e) {
            echo '合成失败: ' . $e->getMessage();
        }
    }
}